<!--车辆处置页面-->
<template>
  <div>
    <el-dialog
      :title="doEdit?'操作':'查看'"
      ref="tableDialog"
      :visible.sync="dialogVisible"
      width="80%"
      v-if="formData"
      @close="close"
    >
      <el-form :model="formData" label-width="120px" ref="form">
        <el-row>
          <el-col :sm="12">
            <el-form-item label="申请人单位">{{formData.company.companyName}}</el-form-item>
          </el-col>
          <el-col :sm="12">
            <el-form-item label="申请车辆">{{formData.car.carCode}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :sm="24">
            <el-form-item label="维保方式">{{formData.typeStr}}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="24">
            <el-form-item label="申请原因">{{formData.reason}}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="24">
            <el-form-item label="附件" style="color:red;">
              <a @click="download(formData.fileurl1,formData.filename1)">{{formData.filename1}}</a>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="历史流程">
            <el-table :data="history" style="width: 100%" size="mini" border>
              <el-table-column prop="taskName" label="步骤" align="center"></el-table-column>
              <el-table-column prop="userName" label="审核人" align="center"></el-table-column>
              <el-table-column prop="mobile" label="电话" align="center"></el-table-column>
              <el-table-column prop="mark" label="审批意见" align="center"></el-table-column>
              <el-table-column prop="passStr" label="状态" align="center"></el-table-column>
              <el-table-column prop="optime" label="操作时间" align="center"></el-table-column>
            </el-table>
          </el-form-item>
        </el-row>
      </el-form>
      <el-form label-width="80px" v-if="doEdit&&type==0">
        <el-row>
          <el-col :sm="24">
            <el-form-item label="是否通过">
              <el-radio-group v-model="pass">
                <el-radio :label="2">同意并办结</el-radio>
                <el-radio :label="1">同意并上报</el-radio>
                <el-radio :label="0">不同意</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-show="sprListData&&sprListData.length>0&&pass==1">
          <el-col>
            <el-form-item label="审批人" prop="shrid">
              <el-select v-model="shrid" placeholder="请选择审批人" style="width: 100%;">
                <el-option
                  :label="item.username"
                  :value="item.userid"
                  v-for="(item, index) in sprListData"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top: 10px;">
          <el-col :sm="24">
            <el-form-item label="说明">
              <el-input v-model="formData.shenhemark" type="textarea" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer" style="text-align: left;">
        <el-button @click="close">关闭</el-button>
        <el-button type="primary" @click="save" v-if="doEdit&&type==0">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { viewJson, sh } from "@/api/task";
import { sprListJson } from "@/api/weixiu";
import { dateFormat } from "@/utils/utils";
export default {
  data() {
    return {
      pass: 2,
      opData: null,
      history: [],
      formData: null,
      shrid: null,
      doEdit: false,
      dialogVisible: false,
      type: 0, //0待办任务 2历史任务
      sprListData: [], //审核人列表
    };
  },
  methods: {
    close() {
      this.dialogVisible = false;
    },
    /**
     * @description 操作任务
     * @param opData 当前操作的任务原始数据 list接口返回的
     * @param type 当前列表的类型 0待办任务 2历史任务 2类型时候，只有调度员可编辑且不能再修改备注了
     */
    editTask(opData, type) {
      this.viewTask(opData, type);
      this.doEdit = true;
      this.tabIndex = "3";
      this.type = type;
    },
    /**
     * @description 查看任务
     * @param opData 当前操作的任务原始数据 list接口返回的
     * @param type 当前列表的类型 0待办任务 2历史任务 2类型时候，只有调度员可编辑且不能再修改备注了
     */
    async viewTask(opData, type) {
      this.type = type;
      this.doEdit = false;
      this.opData = opData;
      let params = {
        tableId: opData.tableId,
        pid: opData.processInstanceId,
        taskId: opData.taskId,
        formKey: opData.formKey,
      };
      let r = await viewJson(params);
      this.dialogVisible = true;
      this.formData = r.data;
      this.history.splice(0, this.history.length);
      this.history.push(...r.history);
      this.sprListJson();
      console.log(this.formData);
    },
    //获取审批人列表
    async sprListJson() {
      let r = await sprListJson({ tableId: this.opData.tableId });
      this.sprListData.splice(0, this.sprListData.length, ...r);
      if (r && r.length > 0) {
        this.shrid = r[0].userid;
      }
    },
    //处理流程
    save() {
      if (this.sprListData.length > 0 && !this.shrid) {
        this.$message({
          message: "请选择审批人",
          type: "error",
        });
        return;
      }
      let param = { shrid: this.shrid, pass: this.pass };
      param["mark"] = this.formData.shenhemark || "";
      param["tableId"] = this.opData.tableId;
      param["taskId"] = this.opData.taskId;
      param["formKey"] = this.opData.formKey;
      param["pid"] = this.opData.processInstanceId;
      this.sh(param);
    },
    //审核
    async sh(params) {
      let d = await sh(params);
      if (d.error == 0) {
        this.$message("操作成功");
        this.finish();
      } else {
        this.$message("操作失败");
      }
    },
    //各种操作完成后关闭对话框刷新父组件列表
    finish() {
      this.close();
      this.$parent.fetchData();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-timeline {
  position: relative;
  top: -25px;
  left: 80px;
  margin-bottom: -45px;
}
a {
  color: blue;
}
</style>